/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Entity.Item;
import Entity.Receipt;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Trung Duc
 */
public class ReceiptTableModel extends AbstractTableModel {
    private List<Receipt> listReceipt;
    ResourceBundle messages;

    public ReceiptTableModel(List<Receipt> listReceipt, ResourceBundle messages) {
        this.listReceipt = listReceipt;
        this.messages = messages;
    }

    public ReceiptTableModel(ResourceBundle messages) {
        this.messages = messages;
        listReceipt = new ArrayList<Receipt>();
    }
    public boolean add(Receipt receipt){
        return listReceipt.add(receipt);
    }
    @Override
    public int getRowCount() {
        return listReceipt.size();
    }

    @Override
    public int getColumnCount() {
        return 6;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Receipt r = listReceipt.get(rowIndex);
        Timestamp timestamp = new Timestamp(r.getDate().getTime());
        String date = timestamp.toString();
        switch(columnIndex){
            case 0: return r.getBillID();
            case 1: return r.getAccount();
            case 2: return r.getItemName();
            case 3: return r.getCategoryName();
            case 4: return r.getQuantity();
            case 5: return date;
            default : return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch(column){
            case 0: return messages.getString("billIDTable");
            case 1: return messages.getString("account");
            case 2: return messages.getString("itemName");
            case 3: return messages.getString("itemCategory");
            case 4: return messages.getString("itemQuantity");
            case 5: return messages.getString("date");
            default : return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch(columnIndex){
            case 0: return Integer.class;
            case 1: return String.class;
            case 2: return String.class;
            case 3: return String.class;
            case 4: return Integer.class;
            case 5: return String.class;
            default : return null;
        }
    }

    @Override
    public void fireTableDataChanged() {
        super.fireTableDataChanged();
    }
    
    public int getAmountProductReturn(){
        int count = 0;
        for (Iterator<Receipt> it = listReceipt.iterator(); it.hasNext();) {
            Receipt receipt = it.next();
            count+=receipt.getQuantity();
        }
        return count;
    }
    public Receipt getReceiptByBillID(int billID){
        for (Iterator<Receipt> it = listReceipt.iterator(); it.hasNext();) {
            Receipt receipt = it.next();
            if(receipt.getBillID() == billID)
                return receipt;
        }
        return null;
    }
}
